#include "norm_steptest.h"

#include "ct_itemdrawable/ct_scene.h"
#include "ct_pointcloudindex/ct_pointcloudindexvector.h"
#include "ct_itemdrawable/tools/iterator/ct_groupiterator.h"
#include "ct_result/ct_resultgroup.h"
#include "ct_result/model/inModel/ct_inresultmodelgroup.h"

#include "noeud.h"
#include "octree.h"

// Alias for indexing in models
#define DEF_resultIn_inputResult "inputResult"
#define DEF_groupIn_inputScene "inputGroup"
#define DEF_itemIn_scene "inputScene"

#include <QDebug>

// Constructor : initialization of parameters
NORM_StepTest::NORM_StepTest(CT_StepInitializeData &dataInit) : CT_AbstractStep(dataInit)
{
}

// Step description (tooltip of contextual menu)
QString NORM_StepTest::getStepDescription() const
{
    return tr("toto");
}

// Step detailled description
QString NORM_StepTest::getStepDetailledDescription() const
{
    return tr("titi");
}

// Step URL
QString NORM_StepTest::getStepURL() const
{
    //return tr("STEP URL HERE");
    return CT_AbstractStep::getStepURL(); //by default URL of the plugin
}

// Step copy method
CT_VirtualAbstractStep* NORM_StepTest::createNewInstance(CT_StepInitializeData &dataInit)
{
    return new NORM_StepTest(dataInit);
}

//////////////////// PROTECTED METHODS //////////////////

// Creation and affiliation of IN models
void NORM_StepTest::createInResultModelListProtected()
{
    CT_InResultModelGroup *resIn_ResultScene = createNewInResultModel(DEF_itemIn_scene);
    resIn_ResultScene->setRootGroup(DEF_groupIn_inputScene);
    resIn_ResultScene->addItemModel(DEF_groupIn_inputScene, DEF_itemIn_scene, CT_Scene::staticGetType());

}

// Creation and affiliation of OUT models
void NORM_StepTest::createOutResultModelListProtected()
{
    // No OUT model definition => create an empty result
    createNewOutResultModel("emptyResult");
}

// Semi-automatic creation of step parameters DialogBox
void NORM_StepTest::createPostConfigurationDialog()
{
    // No parameter dialog for this step
}



void NORM_StepTest::compute()
{
    CT_ResultGroup* resultIn_inputResult = getInputResults().first();
    CT_Scene* itemIn_scene;
    CT_ResultItemIterator it(resultIn_inputResult, this, DEF_itemIn_scene);
    Noeud* noeud;
    Octree* octree;

    if (it.hasNext())
     {
        itemIn_scene = (CT_Scene*)it.next();
     }
    /******************************************************************************
              *      User's Compute
    ******************************************************************************/
    // On recupere le nuage de points de la scene en entree et son nombre de points
    CT_ABSTRACT_PCIR inputPointCloud = itemIn_scene->getPointCloudIndexRegistered();
    size_t i, nbPoints = inputPointCloud->size();



    CT_Point curPoint;
    CT_PointCloudIndexVector* indexPoint = new CT_PointCloudIndexVector();


    for ( i = 0 ; i < nbPoints ; i++ )
    {
        indexPoint->addIndex( inputPointCloud->indexAt(i) );
    }
    CT_ABSTRACT_MPCIR indexPointRe = PS_REPOSITORY->registerPointCloudIndex(indexPoint);

    scanf("Cb de pts min par cellule ?",&noeud->nbPtsMin);

    noeud->subdivide(indexPointRe,noeud->pere,itemIn_scene);

/*
    for(int i=0;i<nbPoints;i++){
    qDebug()<<"point : "<<indexPoint->indexAt(i)<<"Coord X "<<indexPoint->tAt(i)(0)<<indexPoint->indexAt(i)<<"Coord Y "<<indexPoint->tAt(i)(1)<<indexPoint->indexAt(i)<<"Coord Z "<<indexPoint->tAt(i)(2);
}

    qDebug()<<a <<b <<c << d<< e<< f<< g;

    for(i=0;i<a;i++){

        curPoint=indexPoint->tAt(i);
        qDebug()<<"Partie A" << curPoint(0) << curPoint(1) << curPoint(2);

    }
    for(i=a;i<b;i++){

        curPoint=indexPoint->tAt(i);
        qDebug()<<"Partie B" << curPoint(0) << curPoint(1) << curPoint(2);

    }

    for(i=b;i<c;i++){

        curPoint=indexPoint->tAt(i);
        qDebug()<<"Partie C" << curPoint(0) << curPoint(1) << curPoint(2);

    }

    for(i=c;i<d;i++){

        curPoint=indexPoint->tAt(i);
        qDebug()<<"Partie D" << curPoint(0) << curPoint(1) << curPoint(2);

    }

    for(i=d;i<e;i++){

        curPoint=indexPoint->tAt(i);
        qDebug()<<"Partie E" << curPoint(0) << curPoint(1) << curPoint(2);

    }

    for(i=e;i<f;i++){

        curPoint=indexPoint->tAt(i);
        qDebug()<<"Partie F" << curPoint(0) << curPoint(1) << curPoint(2);

    }
    for(i=f;i<g;i++){

        curPoint=indexPoint->tAt(i);
        qDebug()<<"Partie G" << curPoint(0) << curPoint(1) << curPoint(2);

    }

    for(i=g;i<nbPoints;i++){

        curPoint=indexPoint->tAt(i);
        qDebug()<<"Partie H" << curPoint(0) << curPoint(1) << curPoint(2);

    }

}


*/
}
